/**
 * Created by linweihao on 15/12/23.
 */
var Promise = require('bluebird')
    , Request = Promise.promisify(require("request"))
    , _ = require('lodash')

function RequestSina(name) {
    var url = "http://stock.finance.sina.com.cn/futures/api/json.php/InnerFuturesService.getInnerFuturesDailyKLine?symbol=" + name
    return new Promise(function (resolve, reject) {
        Request.get({url: url}, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                resolve(body)
            } else {
                reject(err)
            }
        })
    })
}


function average(arr, averageDays, days) {
    var count = arr.length
        , num = averageDays || 5
    if (days) {
        arr = _.takeRight(arr, days)
    }
    arr.forEach(function (obj, i) {
        if (i >= num - 1) {
            var sum = 0
            for (var j = i; (i - j) < num; j--) {
                sum = sum + parseFloat(arr[j].close)
            }
            var average = sum / num
            obj.average = average
        }
    })
    return arr
}


function calculate(arr, isRise) {
    var rise = false, recordPosition, count = 0, total = 0, startCaluate = false, endCaluate = false, countP = 0
        , result = {
            count: 0,
            total: 0,
            days: 0
        }
    arr.forEach(function (obj, i) {
        if (obj.average) {
            var differ = obj.close - obj.average
            //console.log('diff....' + differ + "....i...." + i)
            if (isRise) {
                rise = (differ > 0 ) ? true : false
            } else {
                rise = (differ < 0 ) ? true : false
            }

            if (rise) {
                if (!startCaluate) {
                    startCaluate = true
                    recordPosition = i
                }
            } else {
                if (startCaluate && !endCaluate) {
                    endCaluate = true
                }
            }

            if (startCaluate && endCaluate) {
                count = count + 1
                var start = arr[recordPosition + 1].open + 1
                    , endPositon = (i + 1) >= arr.length ? true : false
                if (endPositon) {
                    return
                }
                var end = arr[i + 1].open - 1
                    , income = end - start
                console.log('income....' + income + '.....position....' + recordPosition + " ..high.." + arr[recordPosition + 1].high + '..low...' + arr[recordPosition + 1].low + "..open..." + arr[recordPosition + 1].open +
                    "...i..." + i + " ..high.." + arr[i + 1].high + '..low...' + arr[i + 1].low + "..open..." + arr[i + 1].open)
                total = total + income
                startCaluate = false
                endCaluate = false
            }
        }
    })
    result.days = arr.length
    result.count = count
    result.total = total
    if (!isRise) {
        result.total = -total
    }
    return result
}

Promise.resolve()
    .then(function () {
        return RequestSina("MA0")
    }).then(function (array) {
        return average(eval('(' + array + ')'), 5, 200)
    }).then(function (a) {
        return calculate(a, false)
    }).then(function (result) {
        console.log(result)
    })


